package com.demo2do.fms.admin.web.controller.data.car;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.fms.admin.service.DataService;
import com.demo2do.fms.admin.utils.FormatUtils;
import com.demo2do.fms.admin.web.view.ExcelTemplateView;
import com.demo2do.fms.entity.data.car.Manufacturer;

/**
 * 查询商品车制造商代码
 * 
 * @author Wy
 *
 */
@Controller
@RequestMapping("/manufacturers")
@MenuSetting("menu-data")
public class ManufacturerController {
	
	@Autowired
	@Qualifier("dataService")
    private DataService<Manufacturer> manufacturerService;
	
	/**
	 * 
	 * @param page
	 * @return
	 */
	@MenuSetting("submenu-manufacturer")
	@RequestMapping(value = "", method = RequestMethod.GET)
	public ModelAndView list(Page page){
	    
		ModelAndView modelAndView = new ModelAndView("data/car/manufacturer-list");
		
		int totals = manufacturerService.count(Manufacturer.class);
		if(totals > 0) {
			
			List<Manufacturer> manufacturers = manufacturerService.listByPage(Manufacturer.class, page.with(totals));
			
			modelAndView.addObject("page", page);
            modelAndView.addObject("manufacturers", manufacturers);
		}
		
		return modelAndView;
		
	}
	
    /**
     * 输出当前页记录/输出所有记录
     * 
     * @param page
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView export(Page page) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("data/car/manufacturer-export.xlsx"));
        
        // 1. list result
		int totals = manufacturerService.count(Manufacturer.class);
		if(totals > 0){
			List<Manufacturer> manufacturers = manufacturerService.listByPage(Manufacturer.class, page.with(totals));
            modelAndView.addObject("manufacturers", manufacturers);
		}
		
        // 2. format util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
		
		return modelAndView;
		
    }

}
